package com.creating.mict.action;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.creating.mict.common.DateUtils;
import com.creating.mict.domain.Patient;
import com.creating.mict.domain.User;
import com.creating.mict.domain.XtyRecord;
import com.creating.mict.domain.XyjRecord;
import com.creating.mict.service.IAndroidService;
import com.creating.mict.service.IOfficeService;
import com.creating.mict.service.IPatientService;
import com.creating.mict.service.IUserService;
import com.creating.mict.service.IXtyService;
import com.creating.mict.service.IXyjService;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class AndroidAction extends ActionSupport {
	//wzu
	@Resource
	private IUserService userService;
	@Resource
	private IOfficeService officeService;
	@Resource
	private IPatientService patientService;
	@Resource
	private IXyjService xyjService;
	@Resource
	private IXtyService xtyService;
	@Resource
	private IAndroidService androidService;
	
	public void login() throws Exception
	{
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		User user = new User();
		user = userService.getUserByLoginName(loginName);
		if(user == null ||  !(user.getPassword().equals(password))) {
			out.print("{loginFlag:failure}");
			return;
		}
		String office = user.getFoffice();
		if(user.getPassword().equals(password)){
			out.print("{loginFlag:success,office:'"+office+"'}");
		}
	}
	
	public void queryPatientSigned() throws Exception
	{
		//what parameters we need , loginName, loginOffice, loginType
		HttpServletRequest request = ServletActionContext.getRequest();
		//common user
		String loginType = "1";
		loginName = request.getParameter("loginName");
		List<Patient> patientList = patientService.queryPatientSigned(loginName, loginType, null, null, null, null, null); //全部查询，包括隐藏信息
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>").append("\r\n").append("<Patients>").append("\r\n");
		for(int i = 0; i<patientList.size(); i++) {
			sb.append("	<Patient>").append("\r\n")
				.append("		<pId>").append(patientList.get(i).getFpid()).append("</pId>").append("\r\n")
				.append("		<pName>").append(patientList.get(i).getFname()).append("</pName>").append("\r\n")
				.append("		<pSex>").append(patientList.get(i).getFsex()).append("</pSex>").append("\r\n")
				.append("		<pPhone>").append(patientList.get(i).getFphone1()).append("</pPhone>").append("\r\n")
				.append("		<pGroup>").append(patientList.get(i).getFgroup()).append("</pGroup>").append("\r\n")
				.append("		<pBeginTime>").append(patientList.get(i).getFbdateStr()).append("</pBeginTime>").append("\r\n")
				.append("		<pEndTime>").append(patientList.get(i).getFedateStr()).append("</pEndTime>").append("\r\n")
			.append("	</Patient>").append("\r\n");
		}
		sb.append("</Patients>").append("\r\n");
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		out.print(sb.toString());
	}
	
	public void addPatient() throws Exception
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		String startTime  = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String sexSpinner = request.getParameter("sexSpinner");
		String identytyNum = request.getParameter("identytyNum");
		String healthCardNum = request.getParameter("healthCardNum");
		String pName = request.getParameter("pName");
		String pContactPhoneNum = request.getParameter("pContactPhoneNum");
		String pNormalPhoneNum = request.getParameter("pNormalPhoneNum");
		String pHomeAddress = request.getParameter("pHomeAddress");
		
		String ps_blood_pressure  = request.getParameter("ps_blood_pressure");
		String ps_blood_sugar  = request.getParameter("ps_blood_sugar");
		String ps_blood_fat  = request.getParameter("ps_blood_fat");
		String ps_blood_uric_acid  = request.getParameter("ps_blood_uric_acid");
		String ps_blood_serum_creatinine  = request.getParameter("ps_blood_serum_creatinine");
		String ps_blood_oxygen_saturation  = request.getParameter("ps_blood_oxygen_saturation");
		String ps_eye_ground  = request.getParameter("ps_eye_ground");
		String ps_electrocardio  = request.getParameter("ps_electrocardio");
		String ps_position  = request.getParameter("ps_position");
		
		Patient pa = new Patient();
		pa.setFbdate(DateUtils.parseDate(startTime));
		pa.setFedate(DateUtils.parseDate(endTime));
		pa.setFpid(DateUtils.getTransactionIdByLongTime());
		pa.setFsign_date(new Date());
		pa.setFsign_doctor(loginName);
		pa.setFidcd(identytyNum);
		pa.setFybkh(healthCardNum);
		pa.setFname(pName);
		pa.setFsex(sexSpinner);
		pa.setFphone1(pContactPhoneNum);
		pa.setFphone2(pNormalPhoneNum);
		pa.setFadd_detail(pHomeAddress);
		
		pa.setFxyjc(ps_blood_pressure);
		pa.setFxtjc(ps_blood_sugar);
		pa.setFxzjc(ps_blood_fat);
		pa.setFxnsjc(ps_blood_uric_acid);
		pa.setFxjgjc(ps_blood_serum_creatinine);
		pa.setFxybhdjc(ps_blood_oxygen_saturation);
		pa.setFydjc(ps_eye_ground);
		pa.setFxdjc(ps_electrocardio);
		pa.setFwzfw(ps_position);
		pa.setFstate("1");
	
		boolean success = patientService.savePatientInfo(pa);
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		out.print(success==true?"success":"false");
	}

	/**
	 * 
	* @Title: queryBloodPressure 
	* @Description: 获取血压计测量值
	* @param @return 
	* @return 
	* @throws
	 */
	
	public void queryBloodPressure() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String fphone1 = request.getParameter("fphone1");
		
		String fetime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		
		List<XyjRecord> list = androidService.produceBloodPressureToChart(null, fetime, fphone1);
		
		StringBuffer jsonStr = new StringBuffer("[");
		for (int i = 0; i < list.size(); i++)
		{
			XyjRecord xr = new XyjRecord();
			xr = list.get(i);
			jsonStr.append("{");
			jsonStr.append("fssy:'").append(xr.getFssy()).append("',");
			jsonStr.append("fszy:'").append(xr.getFszy()).append("',");
			jsonStr.append("fxl:'").append(xr.getFxl()).append("',");
			jsonStr.append("frdate:'").append(xr.getFrdateStr()).append("'");
			
			if (i == list.size() - 1)
				jsonStr.append("}");
			else
				jsonStr.append("},");
		}
		jsonStr.append("]");
		String str = "{totalProperty: " + list.size() + ", root:" + jsonStr + "}";
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		out.print(str);
	}
	
	/**
	 *  for monitor information
	 * @throws Exception
	 */
	public void queryBloodPressureRecord() throws Exception{

		HttpServletRequest request = ServletActionContext.getRequest();
		
		loginName = request.getParameter("loginName");
		String loginOffice = request.getParameter("loginOffice");
		String fetime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		
		List<XyjRecord> list = xyjService.queryPatientXyj(loginName, loginOffice, "", fetime, "0", "", "false");
		
		StringBuffer jsonStr = new StringBuffer("[");
		for (int i = 0; i < list.size(); i++)
		{
			XyjRecord xr = new XyjRecord();
			xr = list.get(i);
			jsonStr.append("{");
			jsonStr.append("fpid:'").append(xr.getFpid()).append("',");
			jsonStr.append("fname:'").append(xr.getFname()).append("',");
			jsonStr.append("fsex:'").append(xr.getFsex()).append("',");
			jsonStr.append("fphone1:'").append(xr.getFphone()).append("',");
			jsonStr.append("fssy:'").append(xr.getFssy()).append("',");
			jsonStr.append("fszy:'").append(xr.getFszy()).append("',");
			jsonStr.append("fxl:'").append(xr.getFxl()).append("',");
			jsonStr.append("frdate:'").append(xr.getFrdateStr()).append("'");
			
			if (i == list.size() - 1)
				jsonStr.append("}");
			else
				jsonStr.append("},");
		}
		jsonStr.append("]");
		
		String str = "{list: " + jsonStr +"}";
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		out.print(str);
	}
	
	
	
	/**
	 * 
	* @Title: queryBloodSugar 
	* @Description: 获取血糖计测量值
	* @param @return 
	* @return 
	* @throws
	 */
	public void queryBloodSugar() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String fphone1 = request.getParameter("fphone1");
		
		String fetime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		
		List<XtyRecord> list0 = androidService.produceXTBeforeLunchToChart(null, fetime, fphone1);
		List<XtyRecord> list1 = androidService.produceXTAfterLunchOneHourToChart(null, fetime, fphone1);
		List<XtyRecord> list2 = androidService.produceXTAfterLunchTwoHoursToChart(null, fetime, fphone1);
		
		
		//餐前
		StringBuffer jsonStr0 = new StringBuffer("[");
		for (int i = 0; i < list0.size(); i++)
		{
			XtyRecord xr = new XtyRecord();
			xr = list0.get(i);
			jsonStr0.append("{");
			jsonStr0.append("cqxt:'").append(xr.getFcqxt()).append("',");
			jsonStr0.append("frdate:'").append(xr.getFrdateStr()).append("'");
			
			if (i == list0.size() - 1)
				jsonStr0.append("}");
			else
				jsonStr0.append("},");
		}
		jsonStr0.append("]");
		
		//餐后1小时
		StringBuffer jsonStr1 = new StringBuffer("[");
		for (int i = 0; i < list1.size(); i++)
		{
			XtyRecord xr = new XtyRecord();
			xr = list1.get(i);
			jsonStr1.append("{");
			jsonStr1.append("chxt1:'").append(xr.getFchxt1()).append("',");
			jsonStr1.append("frdate:'").append(xr.getFrdateStr()).append("'");
			
			if (i == list1.size() - 1)
				jsonStr1.append("}");
			else
				jsonStr1.append("},");
		}
		jsonStr1.append("]");
				
		//餐后2小时
		StringBuffer jsonStr2 = new StringBuffer("[");
		for (int i = 0; i < list2.size(); i++)
		{
			XtyRecord xr = new XtyRecord();
			xr = list2.get(i);
			jsonStr2.append("{");
			jsonStr2.append("chxt2:'").append(xr.getFchxt2()).append("',");
			jsonStr2.append("frdate:'").append(xr.getFrdateStr()).append("'");
			
			if (i == list2.size() - 1)
				jsonStr2.append("}");
			else
				jsonStr2.append("},");
		}
		jsonStr2.append("]");
		
		
		String str = "{list0: " + jsonStr0 + ", list1:" + jsonStr1 + ", list2:"+ jsonStr2 +"}";
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		out.print(str);
	}
	
	/**
	 *  for monitor information 
	 * @throws Exception
	 */
	public void queryBloodSugarRecord() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		
		loginName = request.getParameter("loginName");
		String loginOffice = request.getParameter("loginOffice");
		String fetime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		
		List<XtyRecord> list = xtyService.queryPatientXty(loginName, loginOffice, "", fetime, "0", "", "false");
		
		StringBuffer jsonStr = new StringBuffer("[");
		for (int i = 0; i < list.size(); i++)
		{
			XtyRecord xr = new XtyRecord();
			xr = list.get(i);
			jsonStr.append("{");
			jsonStr.append("fpid:'").append(xr.getFpid()).append("',");
			jsonStr.append("fname:'").append(xr.getFname()).append("',");
			jsonStr.append("fsex:'").append(xr.getFsex()).append("',");
			jsonStr.append("fphone1:'").append(xr.getFphone()).append("',");
			jsonStr.append("fcqxt:'").append(xr.getFcqxt()).append("',");
			jsonStr.append("fchxt1:'").append(xr.getFchxt1()).append("',");
			jsonStr.append("fchxt2:'").append(xr.getFchxt2()).append("',");
			jsonStr.append("frdate:'").append(xr.getFrdateStr()).append("'");
			
			if (i == list.size() - 1)
				jsonStr.append("}");
			else
				jsonStr.append("},");
		}
		jsonStr.append("]");
		
		String str = "{list: " + jsonStr +"}";
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		out.print(str);
	}
	
	
	private String loginName;
	private String password;

	
	/**
	 * @return the loginName
	 */
	public String getLoginName() {
		return loginName;
	}

	/**
	 * @param loginName the loginName to set
	 */
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
